import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from '../../environments/environment';
import {Observable} from 'rxjs';
import {VAILDATOR_COMMON} from './form-validator';

@Injectable()
export class CommonService {
  // 连续提交等待时间

  constructor(private http: HttpClient) {
  }
  fileTypes;
  getLocalStorageFromJson(callback?: Function) {
    this.http.get('assets/data/config.json')
      .subscribe(v => {
        const httpDomain = v['httpDomain'];
        localStorage.setItem('httpDomain', httpDomain);
        const projectName = v['projectName'];
        localStorage.setItem('projectName', projectName);
        const orgId = v['orgId'];
        localStorage.setItem('orgId', orgId);
        const orgConfig = `${environment.localStoragePrefix}.proConfig.org` + orgId;
        localStorage.setItem(`orgConfig`, orgConfig);
        const ws = (location.protocol === 'https:' ? 'wss://' : 'ws://') + v['ws'];
        localStorage.setItem('ws', ws);
        this.http.post(httpDomain + 'getProConfigById', orgId).subscribe(n => {
          if (n['code'] === 0) {
            localStorage.setItem(orgConfig, JSON.stringify(n['data']));
          } else {
            console.log('错误', '项目配置对象请求失败！', 'error');
          }
          if (callback) {
            callback();
          }
        }, () => {
          console.log('警告', '项目配置对象请求异常！', 'warning');
        });
      });
  }

  /**
   * 异步方法
   */
  getCommonSelectInfo(url: string, shortMsg: string): any {
    return Observable.create((observer) => {
      this.http.get(localStorage.getItem(`httpDomain`) + `${url}`)
        .subscribe(v => {
            if (v['code'] !== 0) {
              console.log('警告', `${shortMsg}获取异常！`, 'warning');
              observer.next(null);
            } else {
              observer.next(v['data']);
            }
            observer.complete();
          },
          () => {
            console.log('警告', `${shortMsg}获取异常！`, 'warning');
            observer.next(null);
            observer.complete();
          });
    });
  }
  /**
   * work: 格式化姓名与电话
   * time: 2019.3.5
   * author: P.J
   * param filed: 后台返回信息数据
   */
  personAndTel(filed) {
    if (filed) {
      return filed.perName + '(' + filed.mobile + ')';
    } else {
      return '';
    }
  }

  /**
   * 同步方法
   */
  regTest = (reg, value) => reg.test(value);
  // 两位数格式化
  twoDoubleFormatter = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    (Math.round(v * 100) / 100).toFixed(2) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0 :
      '';
  twoDoubleParser = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    (Math.round(v * 100) / 100).toFixed(2) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0 :
      '';

  fourDoubleFormatter = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    (Math.round(v * 10000) / 10000).toFixed(4) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0.0000 :
      '';
  fourDoubleParser = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    (Math.round(v * 10000) / 10000).toFixed(4) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0.0000 :
      '';
  // 六位数格式化
  sixDoubleFormatter = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    (Math.round(v * 1000000) / 1000000).toFixed(6) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0 :
      '';
  sixDoubleParser = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    (Math.round(v * 1000000) / 1000000).toFixed(6) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0 :
      '';
  // 整数格式化
  intFormatter = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    Math.round(v) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0 :
      '';
  intParser = v => v && this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
    Math.round(v) :
    !this.regTest(VAILDATOR_COMMON.NUMBER_ALL_REGEXP, v) ?
      0 :
      '';
  // 判断单个上传文件大小是否符合

  // 判断一个数组中是否存在该对象
  contains = (arr, obj) => {
    let i = arr.length;
    while (i--) {
      if (arr[i] === obj) {
        return true;
      }
    }
    return false;
  }

  // 判断一个数组是否有值
  isNotEmptyArray = l => l != null && l.length !== 0;

}
